// cloudfunctions/getJobs/index.js
const mysql = require('mysql2/promise');

// 数据库配置
// 请替换以下内容为您的实际数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root', // 请替换为您的数据库用户名
  password: '123456', // 请替换为您的数据库密码
  database: 'alumni_job' // 请替换为您的数据库名称
};

exports.main = async (event, context) => {
  const { userId, role, status, keyword, skills } = event;

  try {
    // 创建数据库连接
    const connection = await mysql.createConnection(dbConfig);

    // 构建查询语句
    let query = `SELECT * FROM jobs WHERE status = ?`;
    const params = [status];

    if (keyword) {
      query += ` AND title LIKE ?`;
      params.push(`%${keyword}%`);
    }

    if (skills) {
      query += ` AND skills LIKE ?`;
      params.push(`%${skills}%`);
    }

    // 执行查询
    const [rows] = await connection.execute(query, params);

    // 关闭数据库连接
    await connection.end();

    return {
      success: true,
      data: rows
    };
  } catch (err) {
    console.error('数据库查询失败:', err);
    return {
      success: false,
      error: '数据库查询失败'
    };
  }
};
